package com.lin.controller;


import com.lin.entity.User;
import com.lin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 阿林
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String index(Model model) {

        return "index";
    }

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<User> users = userService.findAll();
        model.addAttribute("users", users);
        return "demo";
    }

    @RequestMapping("/toAddUser")
    public String toAddUser() {
        return "addUser";
    }

    @RequestMapping("/addUser")
    public String addUser(User user) {
        //保存
        userService.addUser(user);
        //回到主页
        return "index";
    }


    @RequestMapping("/toupdateUser")
    public String toupdateUser() {
        return "updateUser";
    }

    @RequestMapping("/updateUser")
    public String updateUser(User user) {
        userService.updateUser(user);

        return "index";
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(int id) {

        userService.deleteById(id);

        return "index";
    }
}
